package com.yyr.callmessage.user.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yyr.callmessage.core.config.SystemConfig;
import com.yyr.callmessage.core.ro.BaseRo;
import com.yyr.callmessage.core.util.SMSUtil;
import com.yyr.callmessage.img.domain.service.VerificationImgService;
import com.yyr.callmessage.user.common.UserError;
import com.yyr.callmessage.user.domain.entity.User;
import com.yyr.callmessage.user.domain.service.UserService;
import com.yyr.callmessage.user.object.qo.GetSMSUserQo;
import com.yyr.callmessage.user.object.qo.RegisterAndLoginUserQo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @Description:  user_user 用户（用户模块）
 * @Author：
 * @Date：2021/6/25
 */
@RestController
@RequestMapping(value = SystemConfig.UserPath)
public class UserUserController {


    @Resource
    private UserService userService;

    @Resource
    private VerificationImgService verificationImgService;

    /**
     * @api {post} /1.0/user/user/sms 获取注册短信验证码
     * @apiName user_user_sms_get
     * @apiGroup user_user
     *
     * @apiParam {String} phone  手机号
     * @apiParam {String} verificationImg  图片验证码
     *
     * @apiUse headerAndAuthorization
     * @apiUse successResponse
     */
    @GetMapping(value = "/user/sms")
    public BaseRo getSMSVerification(@RequestBody @Valid GetSMSUserQo qo , HttpServletRequest request) throws Exception {
        //判断图片验证码是否与缓存的一致
        String verificationImg = verificationImgService.getVerificationImg(qo.getPhone());
        if (verificationImg == null || !qo.equals(verificationImg)){
            return new BaseRo(UserError.VerificationImgErrorCode , UserError.VerificationImgErrorMsg);
        }

        //判断该手机号是否注册
        User user = userService.getByPhone(qo.getPhone());
        if (null != user){
            return new BaseRo(UserError.UserPhoneHasExistCode , UserError.UserPhoneHasExistMsg);
        }

        //随机生成四位数并调用第三方接口
        String randomParam = userService.getRandomNumber();
        String resultJson = SMSUtil.sendVerification(qo.getPhone() , randomParam);
        Gson gson = new GsonBuilder().create();
        BaseRo result = gson.fromJson(resultJson , BaseRo.class);
        //短信返回码为0，则发送成功
        if (!"0".equals(result.getCode())){
            return new BaseRo(UserError.UserThirdSMSErrorCode , UserError.UserThirdSMSErrorMsg);
        }

        //添加短信验证码缓存
        userService.addVerificationSMS(qo.getPhone() , randomParam);
        return new BaseRo();
    }

    /**
     * @api {post} /1.0/user/user 手机注册
     * @apiName user_user_post
     * @apiGroup user_user
     *
     * @apiParam {String} phone  手机号
     * @apiParam {String} verificationSMS  短信验证码
     *
     * @apiUse headerAndAuthorization
     * @apiUse successResponse
     */
    @PostMapping(value = "/user")
    public BaseRo register(@RequestBody @Valid RegisterAndLoginUserQo qo){
        //判断短信验证码是否与缓存的一致
        String verificationSMS = userService.getVerificationSMS(qo.getPhone());
        if (verificationSMS == null || !qo.equals(verificationSMS)){
            return new BaseRo(UserError.VerificationSMSErrorCode , UserError.VerificationSMSErrorMsg);
        }

        //判断该手机号是否注册过
        User user = userService.getByPhone(qo.getPhone());
        if (null != user){
            return new BaseRo(UserError.UserPhoneHasExistCode , UserError.UserPhoneHasExistMsg);
        }
        //用户注册
        User obj = new User();
        obj.setPhone(qo.getPhone());
        userService.add(obj);
        return new BaseRo();
    }

}
